<?php

/**
 * product actions.
 *
 * @package    product
 * @subpackage product
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class productActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  	$this->cTitle = "产品列表";
    //$this->forward('default', 'module');
    //list products
    $offset = sfConfig::get('app_page_offset');
    $page = $this->getRequestParameter('page',1);
    $pager = new sfPropelPager('Product',$offset);
    $order_by = $this->getRequestParameter('orderby','id');
    $cr = new Criteria();
    if($order_by=="id"){
    	$cr->addDescendingOrderByColumn(ProductPeer::ID);
    }else{
    	$cr->addDescendingOrderByColumn(ProductCategoryPeer::ID);
    }
    $flag = $request->getParameter('flag',0);
    if($flag){
    	$cr->add(ProductPeer::FLAG,1);
    }
    $pager->setCriteria($cr);
    $pager->setPeerMethod('ProductPeer::doSelectJoinProductCategory');
    $pager->setPage($page);//Product
    $pager->init();
    $this->page = $page;
    $this->pager = $pager;
    $this->order_by = $order_by;
    $this->total = $pager->getNbResults();
    $this->categories = $pager->getResults(); 
  }
  public function executeCreate(sfWebRequest $request)
  {
  	$err = $this->getRequestParameter('err',0);
  	$this->cTitle = "创建新产品";
  	$cr = new Criteria();
  	$cr->add(ProductCategoryPeer::FID,0);
  	$bClass = ProductCategoryPeer::doSelect($cr);
  	$arr = array();
  	foreach ($bClass as $b){
  		$arr[$b->getId()] = $b->getTitle();
  	}
  	$this->bClass = $arr ; 
  	if($err){
  		$this->error = '产品名称、类别、货号 必须填写。';
  	}else{
  		$this->error = '';
  	}
  }
  public function executeImage(sfWebRequest $request)
  {
  	$this->setLayout(false);
  	$type = $this->getRequestParameter('type',1); //default big
  	$this->form = new FileUploadForm();
  	$this->form->setDefault('itype',$type);
  }
  public function executeUpload(sfWebRequest $request)
  {
  	$this->setLayout(false);
  	$form = new FileUploadForm();
  	//$type = $request->getParameter('type');
  	if($request->isMethod('post')){
  		$form->bind($request->getParameter('cate'),			 
			  		$request->getFiles('cate')
  		);
  		if($form->isValid()){
			$file = $form->getValue('img'); //img
			$type = $request->getParameter('cate');//$form->getValue('itype'); //img type
			//var_dump($request->getParameter('cate'));exit;
		    $filename = rand(1,999).sha1($file->getOriginalName());
	        $extension = $file->getExtension($file->getOriginalExtension());
	        $file->save(sfConfig::get('sf_upload_dir').'/products/'.$filename.$extension);
			$this->filename = $filename.$extension;
			$this->box = $type?'product_ico':'product_img';
		}else{
			exit('图片必须是：gif/png/jpg格式，请关闭本窗口再次点击上传。');
		}
		
  	}else{
  		$this->redirect('product/index');
  	}
  	
  	//$target_dir = sfConfig::get('sf_upload_dir').'/products/';
  	
  }
  public function executeSave(sfWebRequest $request)
  {
  	if($request->isMethod('post')){
	  $title = $request->getParameter('title');//must
	  $category = $request->getParameter('category');//must
	  $city = $request->getParameter('city');
	  $lot_no = $request->getParameter('lot_no');//must
	  $product_img = $request->getParameter('product_img');
	  $product_ico = $request->getParameter('product_ico');
	  $specs = $request->getParameter('specs');
	  $params = $request->getParameter('params');
	  $fprice = $request->getParameter('fprice');
	  $sprice = $request->getParameter('sprice');
	  if(strlen($title)==0 || $category==null || strlen($lot_no)==0){
	  	$this->redirect('product/create?err=1');
	  }else{
	  	$Product = new Product();
	  	$Product->setTitle($title);
	  	$Product->setCategory($category);
	  	$Product->setCity($city);
	  	$Product->setLotNo($lot_no);
	  	$Product->setProductImg($product_img);
	  	$Product->setProductIco($product_ico);
	  	$Product->setSpecs($specs);
	  	$Product->setParams($params);
	  	$Product->setFprice($fprice);
	  	$Product->setSprice($sprice);
	  	$Product->save();
	  	$this->forward('product','index');
	  }
  	}else{
  	  $this->forward('product','index');	
  	}
  }
  public function executeDelete(sfWebRequest $request)
  {
  	$this->cTitle = "删除产品";
  	$id = $this->getRequestParameter('id',0);
  	if($id){
  		$product = ProductPeer::retrieveByPK($id);
  		$product->delete();
  		$this->redirect('product/index');
  	}else{
  		$this->redirect('product/index');
  	}
  }
  public function executeRecommend(sfWebRequest $request)
  {
  	$id = $request->getParameter('id');
  	$product = ProductPeer::retrieveByPK($id);
  	if($product){
  		$flag = $product->getFlag();
  		if($flag){
  			$flag=0;
  		}else{
  			$flag=1;
  		}
  		$product->setFlag($flag);
  		$product->save();
  		$this->redirect('product/index?flag=1');
  	}else{
  		$this->redirect('product/index');  		
  	}
  }
  public function executeEdit(sfWebRequest $request)
  {
  	$err = $this->getRequestParameter('err',0);
  	$this->cTitle = "编辑产品";
  	$id=$this->getRequestParameter('id',0);
  	$product = ProductPeer::retrieveByPK($id);
  	if($product){
  		$this->product = $product;
  		$cate = $product->getProductCategory();
  		$this->cate_title = $cate->getTitle();
  		$cate_fid = $cate->getFid();
  		$cate_bclass=ProductCategoryPeer::retrieveByPK($cate_fid);
  		$cr = new Criteria();
	  	$cr->add(ProductCategoryPeer::FID,0);
	  	$bClass = ProductCategoryPeer::doSelect($cr);
	  	$arr = array();
	  	$arr[0]=$cate_bclass->getTitle();
	  	foreach ($bClass as $b){
	  		$arr[$b->getId()] = $b->getTitle();
	  	}
	  	$this->bClass = $arr ; //bigclass
  		if($err){
  		$this->error = '产品名称、类别、货号 必须填写。';
	  	}else{
	  		$this->error = '';
	  	}
  	}else{
  		$this->redirect('product/index');
  	}  
  }
  public function executeEsave(sfWebRequest $request)
  {  	
  	  if($request->isMethod('post')){
  	  	$id = $request->getParameter('id');
  	  	if($id){
		  $title = $request->getParameter('title');//must
		  $category = $request->getParameter('category');//must
		  $city = $request->getParameter('city');
		  $lot_no = $request->getParameter('lot_no');//must
		  $product_img = $request->getParameter('product_img');
		  $product_ico = $request->getParameter('product_ico');
		  $specs = $request->getParameter('specs');
		  $params = $request->getParameter('params');
		  $fprice = $request->getParameter('fprice');
		  $sprice = $request->getParameter('sprice');
		  if(strlen($title)==0 || $category==null || strlen($lot_no)==0){
		  	$this->redirect('product/edit?err=1');
		  }else{
		  	$Product = ProductPeer::retrieveByPK($id);
		  	$Product->setTitle($title);
		  	$Product->setCategory($category);
		  	$Product->setCity($city);
		  	$Product->setLotNo($lot_no);
		  	$Product->setProductImg($product_img);
		  	$Product->setProductIco($product_ico);
		  	$Product->setSpecs($specs);
		  	$Product->setParams($params);
		  	$Product->setFprice($fprice);
		  	$Product->setSprice($sprice);
		  	$Product->save();
		  	$this->forward('product','index');
		  }
  	  	}
  	}else{
  	  $this->forward('product','index');	
  	}
  	
  }
  
  public function executeSearch(sfWebRequest $request)
  {
  	$err = $request->getParameter('err',0);
  	$this->cTitle = "产品搜索";
  	$cr = new Criteria();
  	$cr->add(ProductCategoryPeer::FID,0);
  	$bClass = ProductCategoryPeer::doSelect($cr);
  	$arr = array();
  	foreach ($bClass as $b){
  		$arr[$b->getId()] = $b->getTitle();
  	}
  	$this->bClass = $arr ; 
  	if($err){
  		$this->err = "请输入关键词后查询";
  	}else{
  		$this->err=0;
  	}
  }  		
  /**
	* condition
	*/
  public function executeSoo(sfWebRequest $request)
  {
  	$this->cTitle = "搜索结果";
  	if($request->isMethod('post')||$request->isMethod('get')){ 		
  		$cr = new Criteria();
	    $title = $this->getRequestParameter('title',0);
	    $cateory = $this->getRequestParameter('category',0);
	    $lot_no = $this->getRequestParameter('lot_no',0);
	    $city = $this->getRequestParameter('city',0);
	    if(!$title && !$cateory && !$city && !$lot_no){
	    	$this->redirect('product/search?err=1');
	    }
	    if($title){
	    	$cr->add(ProductPeer::TITLE,'%'.$title.'%',Criteria::LIKE);
	    }   
	    if($cateory){
	    	$cr->add(ProductPeer::CATEGORY ,$cateory);
	    }    
	    if($city){
	    	$cr->add(ProductPeer::CITY ,$city,Criteria::LIKE);
	    }
	    if($lot_no){
	    	$cr->add(ProductPeer::LOT_NO ,$lot_no);
	    }
	    $this->title = $title;
	    $this->category = $cateory;
	    $this->city = $city;
	    $this->lot_no = $lot_no;
	    
	  	$offset = sfConfig::get('app_page_offset');
	    $page = $this->getRequestParameter('page',1);
	    $pager = new sfPropelPager('Product',$offset);
		
	    $pager->setCriteria($cr);
	    $pager->setPage($page);//Product
	    $pager->init();
	    $this->page = $page;
	    $this->pager = $pager;
	    $this->total = $pager->getNbResults();
	    $this->categories = $pager->getResults(); 
  	}else{
  		$this->redirect('product/search');
  	}
  }
}
